/**
 * @file search_phone.c
 *
 */

/*********************
 *      INCLUDES
 *********************/
#include "lv_watch.h"

#if USE_LV_WATCH_SEARCH_PHONE != 0

#include <stdio.h>

/*********************
 *      DEFINES
 *********************/

/**********************
 *      TYPEDEFS
 **********************/

/**********************
 *  STATIC PROTOTYPES
 **********************/
static void search_phone_right_slide_callback(lv_obj_t * obj);
static void search_phone_cleanup(void);
static void search_phone_anim_run(lv_obj_t * obj);
static void search_phone_anim_play_tick_timeout(lv_anim_t * anim);
static void search_phone_destroy(void);
static void search_phone_prepare_destory(lv_obj_t * activity_obj);
static lv_obj_t * search_phone_create(lv_obj_t * activity_obj);

/**********************
 *  STATIC VARIABLES
**********************/
static uint32_t search_phone_anim_playtime = 0;

/**********************
 *      MACROS
 **********************/

/**********************
 *   GLOBAL FUNCTIONS
***********************/
void search_phone_create_event_cb(lv_obj_t * btn, lv_event_t event)
{
    (void)btn;

    if(LV_EVENT_CLICKED == event) {
        lv_watch_png_cache_all_free();

        lv_obj_t * obj = search_phone_create(NULL);
        //LV_ASSERT_MEM(obj);
        if(obj == NULL) return;
    }
}

/**********************
 *   STATIC FUNCTIONS
 **********************/
static lv_obj_t * search_phone_create(lv_obj_t * activity_obj)
{
    if(activity_obj == NULL) {
        lv_watch_activity_ext_t activity_ext;
        memset(&activity_ext, 0, sizeof(lv_watch_activity_ext_t));
        activity_ext.actId = ACT_ID_SEARCH_PHONE;
        activity_ext.create = search_phone_create;
        activity_ext.prepare_destory = search_phone_prepare_destory;
        activity_obj = lv_watch_creat_activity_obj(&activity_ext);
        LV_ASSERT_MEM(activity_obj);
        if(activity_obj == NULL) return NULL;
    }
    lv_obj_t * obj = lv_watch_obj_create(activity_obj);
    LV_ASSERT_MEM(obj);
    if(obj == NULL) return NULL;
    lv_search_phone_ext_t * ext =
        lv_obj_allocate_ext_attr(activity_obj, sizeof(lv_search_phone_ext_t));
    LV_ASSERT_MEM(ext);
    if(ext == NULL) return NULL;

    lv_watch_obj_set_anim_mode(obj, LV_WATCH_LEFT_OR_RIGHT_SLIDE_CALLBACK);
    lv_watch_obj_set_right_slide_cb(obj, search_phone_right_slide_callback);

    uint32_t length = sizeof(nv_watch_bluetooth_t);
    nv_watch_bluetooth_t * nvm = (nv_watch_bluetooth_t *)lv_mem_alloc(length);
    if(length != UI_NV_Read_Req(NV_SECTION_UI_BLUETOOTH, 0, length, (uint8_t *)nvm)) {
        printf("read bluetooth from nvm error in %s\n",__FUNCTION__);
        lv_mem_free(nvm);
        return NULL;
    }
    if(nvm->on_off) {
        lv_obj_t *img = lv_img_create(obj,NULL);
        LV_ASSERT_MEM(img);
        lv_img_set_auto_size(img, true);
        lv_obj_set_click(img, false);
        lv_watch_obj_add_element(img);
        lv_img_set_src(img, ICON_SEARCH_PHONE_FOR);
        lv_obj_align(img, obj, LV_ALIGN_CENTER, 0, 0);
        ext->search_phone_img = img;

        search_phone_anim_playtime = 0;
        search_phone_anim_run(obj);
#if USE_LV_BLE_DEMO != 0
        ble_fmp_client(true);
#endif
    } else {
        lv_obj_t *label = lv_label_create(obj, NULL);
        lv_obj_add_style(label, LV_LABEL_PART_MAIN, &lv_watch_style_transp);
        lv_label_set_align(label, LV_LABEL_ALIGN_CENTER);
        lv_obj_set_click(label, false);
        lv_watch_obj_add_element(label);
        lv_label_set_text_id(label, WATCH_TEXT_ID_BLE_OPEN);
        lv_obj_align(label, obj, LV_ALIGN_CENTER, 0, 0);
    }
    lv_mem_free(nvm);

    return obj;
}

static void search_phone_right_slide_callback(lv_obj_t * obj)
{
    search_phone_destroy();
}

static void search_phone_cleanup(void)
{
    search_phone_anim_playtime = 0;
#if USE_LV_BLE_DEMO != 0
    uint32_t length = sizeof(nv_watch_bluetooth_t);
    nv_watch_bluetooth_t * nvm = (nv_watch_bluetooth_t *)lv_mem_alloc(length);
    if(length != UI_NV_Read_Req(NV_SECTION_UI_BLUETOOTH, 0, length, (uint8_t *)nvm)) {
        printf("read bluetooth from nvm error in %s\n",__FUNCTION__);
        lv_mem_free(nvm);
        return;
    }
    if(nvm->on_off) {
        ble_fmp_client(false);
    }
    lv_mem_free(nvm);
#endif
}

static void search_phone_destroy(void)
{
    lv_watch_png_cache_all_free();
    search_phone_cleanup();
    lv_obj_del_async(lv_watch_get_activity_obj(ACT_ID_SEARCH_PHONE));
}

static void search_phone_anim_run(lv_obj_t * obj)
{
    lv_anim_t a = {};
    lv_anim_init(&a);
    lv_anim_set_var(&a, obj);
    lv_anim_set_start_cb(&a, search_phone_anim_play_tick_timeout);
    lv_anim_set_time(&a, 1);
    lv_anim_set_repeat_count(&a, LV_ANIM_REPEAT_INFINITE);
    lv_anim_set_repeat_delay(&a, 1000);

    lv_anim_start(&a);
}

static void search_phone_anim_play_tick_timeout(lv_anim_t * anim)
{
    lv_obj_t *obj = anim->var;
    lv_search_phone_ext_t * ext = lv_obj_get_ext_attr(lv_watch_get_activity_obj(ACT_ID_SEARCH_PHONE));
    switch (search_phone_anim_playtime % 4)
    {
    case 0:
        lv_img_set_src(ext->search_phone_img, ICON_SEARCH_PHONE_ONE);
        lv_obj_align(ext->search_phone_img, obj, LV_ALIGN_CENTER, 0, 0);
        break;

    case 1:
        lv_img_set_src(ext->search_phone_img, ICON_SEARCH_PHONE_SEC);
        lv_obj_align(ext->search_phone_img, obj, LV_ALIGN_CENTER, 0, 0);
        break;

    case 2:
        lv_img_set_src(ext->search_phone_img, ICON_SEARCH_PHONE_TRD);
        lv_obj_align(ext->search_phone_img, obj, LV_ALIGN_CENTER, 0, 0);
        break;

    case 3:
        lv_img_set_src(ext->search_phone_img, ICON_SEARCH_PHONE_FOR);
        lv_obj_align(ext->search_phone_img, obj, LV_ALIGN_CENTER, 0, 0);
        break;

    default:
        break;
    }
    if(SEARCH_PHONE_ANIM_PLAY_TIME == search_phone_anim_playtime) {
        search_phone_destroy();
        return;
    }
    search_phone_anim_playtime++;
}

static void search_phone_prepare_destory(lv_obj_t * activity_obj)
{
    lv_watch_png_cache_all_free();
    search_phone_cleanup();
}

#endif /*USE_LV_WATCH_SEARCH_PHONE*/
